/* EasyWay Game Engine
 * Copyright (C) 2006 Daniele Paggi.
 *  
 * Written by: 2006 Daniele Paggi<dshnt@hotmail.com>
 *   
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 * 
 * You should have received a copy of the GNU Library General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/**
 * 
 */
package org.easyway.tiles;

import java.util.ArrayList;

import org.easyway.annotations.editor.Info;
import org.easyway.annotations.editor.Method;
import org.easyway.annotations.editor.X;
import org.easyway.annotations.editor.Y;
import org.easyway.editor.commands.user.GetPoint;
import org.easyway.editor.forms.southPanel.TiledMapPanel;
import org.easyway.interfaces.base.ITexture;

/**
 * @author Daniele Paggi
 * this class is used by the UEditor class<br>
 * 
 */
public class TileManagerEditor {
	
	private TileManagerEditor() {
		// disallow instances
	}

	public static ArrayList<Tile> tiles = new ArrayList<Tile>(50);

	public static void removeTile(String name) {
		if (name==null)
			return;
		final TileManager temp = TileManager.getTileManager(name);
		if (temp == null)
			return;
		new Thread() {
			public void run() {
				while (true) {
					float xy[] = GetPoint.getPoint();
					if (xy == null)
						return;
					int X = (int) xy[2] / (int) temp.getTileWidth();
					int Y = (int) xy[3] / (int) temp.getTileHeight();
					temp.setTile(X, Y, null);
				}

			}
		}.start();
	}
	
	public static void multipleTile(final @Info("tile") Tile tile ) {
		if (TiledMapPanel.tiledMap == null || tile==null) {
			new Exception("multipleTile error");
			return;
			
		}

		new Thread() {
			public void run() {
				while (true) {
					float xy[] = GetPoint.getPoint();
					if (xy == null)
						return;
					int X = (int) xy[2] / (int) TiledMapPanel.tiledMap.getTileWidth();
					int Y = (int) xy[3] / (int) TiledMapPanel.tiledMap.getTileHeight();
					TiledMapPanel.tiledMap.setTile(X, Y, tile);
				}

			}
		}.start();
	}
	
	@Method//
	public static void multipleTile(final @Info("tilemap") String name,
			final @Info("tile") Tile tile) {
		if (tile == null || name == null)
			return;
		final TileManager temp = TileManager.getTileManager(name);
		if (temp == null)
			return;
		new Thread() {
			public void run() {
				while (true) {
					float xy[] = GetPoint.getPoint();
					if (xy == null)
						return;
					int X = (int) xy[2] / (int) temp.getTileWidth();
					int Y = (int) xy[3] / (int) temp.getTileHeight();
					temp.setTile(X, Y, tile);
				}

			}
		}.start();

	}

	@Method//
	public static void addTile(@Info("tilemap") String name,
			@X @Info("x") float x, @Y @Info("y") float y,
			@Info("tile") Tile tile) {
		if (tile == null || name == null)
			return;
		TileManager temp = TileManager.getTileManager(name);
		if (temp == null)
			return;
		int X = (int) x / (int) temp.getTileWidth();
		int Y = (int) y / (int) temp.getTileHeight();
		temp.setTile(X, Y, tile);
	}

	@Method//
	public static void createTile(@Info("Image") ITexture image,
			@Info("name") String name) {
		if (image == null || name == null || name.length() == 0) {
			System.out.println("Error making the tile");
			return;
		}

		Tile tile = new Tile(image, name);
		if (tile == null) {
			System.out.println("Error making the tile");
			return;
		}
		tiles.add(tile);
	}
}
